# **********************************************************************
# * $Id$
# *
# * PostGIS - Spatial Types for PostgreSQL
# * http://postgis.refractions.net
# * 
# * Copyright (C) 2010-2011 Sandro Santilli <strk@keybit.net>
# * Copyright (C) 2008 Mark Cave-Ayland
# * Copyright (C) 2005 Refractions Research Inc.
# *
# * This is free software; you can redistribute and/or modify it under
# * the terms of the GNU General Public Licence. See the COPYING file.
# *
# **********************************************************************

#
# PostGIS PGXS build system
#

POSTGIS_PGSQL_VERSION=@POSTGIS_PGSQL_VERSION@

# NOTE: we can't use MODULE_big or PGXS insists in building a library...
PGIS_MODULE_big=postgis-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
MODULEDIR=contrib/$(PGIS_MODULE_big)

# Files to be copied to the contrib/ directory
DATA_built=topology.sql topology_upgrade_20_minor.sql uninstall_topology.sql

# SQL preprocessor
SQLPP = @SQLPP@

# SQL objects (files requiring pre-processing)
SQL_OBJS = \
  topology.sql \
  topology_upgrade.sql \
  topology_drop_before.sql \
  topology_drop_after.sql

# Extra files to remove during 'make clean'
EXTRA_CLEAN=$(SQL_OBJS) $(SQL_OBJS:.sql=.sql.in)

# PGXS information
PG_CONFIG = @PGCONFIG@ 
PGXS := @PGXS@
include $(PGXS)

# Set PERL _after_ the include of PGXS
PERL=@PERL@

# PGXS override feature. The ability to allow PostGIS to install itself
# in a versioned directory is only available in PostgreSQL >= 8.5. To
# do this by default on older PostgreSQL versions, we need to override
# the existing PGXS targets.
#
# Once PostgreSQL 8.5 becomes the minimum supported version, this entire
# section and its associated Makefile.pgxs should be removed.
PGXSOVERRIDE = @PGXSOVERRIDE@
ifeq ($(PGXSOVERRIDE),1)
	include ../postgis/Makefile.pgxs
endif

# If REGRESS=1 passed as a parameter, change the default install paths
# so that no prefix is included. This allows us to relocate to a temporary
# directory for regression testing.
ifeq ($(REGRESS),1)
        bindir=/bin
        pkglibdir=/lib
        datadir=/share
        datamoduledir=contrib/postgis
endif



# Unfortunately we have to copy this from the PGXS Makefile as it only gets picked up
# if MODULE_big is defined
%.sql: %.sql.in
	sed 's,MODULE_PATHNAME,$$libdir/$*,g' $< >$@

# Generate any .sql.in files from .sql.in.c files by running them through the SQL pre-processor 
%.in: %.in.c
	$(SQLPP) $< | grep -v '^#' > $@
	
#Generate upgrade script by stripping things that can't be reinstalled
#e.g. don't bother with tables, types, triggers, and domains
topology_upgrade.sql:  topology.sql
	$(PERL) -0777 -ne 's/^(CREATE|ALTER) (CAST|OPERATOR|TYPE|TABLE|SCHEMA|DOMAIN|TRIGGER).*?;//msg;print;' $< > $@

topology_upgrade_20_minor.sql:  topology_drop_before.sql topology_upgrade.sql topology_drop_after.sql
	cat $^ > $@

topology.sql.in: sql/sqlmm.sql.in.c sql/populate.sql.in.c sql/polygonize.sql.in.c sql/gml.sql.in.c sql/query/getnodebypoint.sql.in.c sql/query/getedgebypoint.sql.in.c sql/query/getfacebypoint.sql.in.c sql/query/GetRingEdges.sql.in.c sql/query/GetNodeEdges.sql.in.c sql/manage/TopologySummary.sql.in.c sql/manage/CopyTopology.sql.in.c sql/manage/ManageHelper.sql.in.c sql/topoelement/topoelement_agg.sql.in.c sql/topogeometry/type.sql.in.c sql/topogeometry/totopogeom.sql.in.c sql/predicates.sql.in.c ../postgis/sqldefines.h ../postgis_svn_revision.h

uninstall_topology.sql: topology.sql ../utils/create_undef.pl 
	$(PERL) ../utils/create_undef.pl $< $(POSTGIS_PGSQL_VERSION) > $@

check: topology.sql
	# $(MAKE) -C test $@

distclean: clean
	rm -f Makefile
